import { create } from 'zustand'

interface LightConfig {
  ambientIntensity: number
  directionalIntensity: number
  directionalPosition: [number, number, number]
}

interface LightStore {
  lightConfig: LightConfig
  setAmbientIntensity: (v: number) => void
  setDirectionalIntensity: (v: number) => void
  setDirectionalPositionX: (x: number) => void
  setDirectionalPositionY: (y: number) => void
  setDirectionalPositionZ: (z: number) => void
  resetLight: () => void
}

const defaultConfig: LightConfig = {
  ambientIntensity: 0.5,
  directionalIntensity: 1,
  directionalPosition: [10, 10, 5],
}

export const useLightStore = create<LightStore>((set) => ({
  lightConfig: defaultConfig,

  setAmbientIntensity: (v) => {
    set((state) => ({
      lightConfig: {
        ...state.lightConfig,
        ambientIntensity: Math.max(0, Math.min(1, v)),
      },
    }))
  },

  setDirectionalIntensity: (v) => {
    set((state) => ({
      lightConfig: {
        ...state.lightConfig,
        directionalIntensity: Math.max(0, Math.min(2, v)),
      },
    }))
  },

  setDirectionalPositionX: (x) => {
    set((state) => ({
      lightConfig: {
        ...state.lightConfig,
        directionalPosition: [x, state.lightConfig.directionalPosition[1], state.lightConfig.directionalPosition[2]],
      },
    }))
  },

  setDirectionalPositionY: (y) => {
    set((state) => ({
      lightConfig: {
        ...state.lightConfig,
        directionalPosition: [state.lightConfig.directionalPosition[0], y, state.lightConfig.directionalPosition[2]],
      },
    }))
  },

  setDirectionalPositionZ: (z) => {
    set((state) => ({
      lightConfig: {
        ...state.lightConfig,
        directionalPosition: [state.lightConfig.directionalPosition[0], state.lightConfig.directionalPosition[1], z],
      },
    }))
  },

  resetLight: () => {
    set({ lightConfig: defaultConfig })
  },
}))

